from PyQt6.QtWidgets import (
    QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton,
    QTextEdit, QGroupBox, QScrollArea
)
from PyQt6.QtCore import Qt, QTimer
from PyQt6.QtGui import QFont
import datetime

class LogWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()
        self.setup_timer()
    
    def init_ui(self):
        """初始化日志界面"""
        self.setStyleSheet("""
            QWidget {
                background-color: white;
                color: black;
                font-family: 'Microsoft YaHei', Arial, sans-serif;
            }
            QGroupBox {
                font-weight: bold;
                color: black;
                background-color: white;
                border: 1px solid #ccc;
                border-radius: 6px;
                margin-top: 8px;
                padding-top: 8px;
            }
            QGroupBox::title {
                subcontrol-origin: margin;
                left: 10px;
                padding: 0 5px 0 5px;
                color: black;
            }
        """)
        
        main_layout = QVBoxLayout(self)
        main_layout.setContentsMargins(15, 15, 15, 15)
        main_layout.setSpacing(10)
        
        # 标题和导出按钮
        header_layout = QHBoxLayout()
        
        title_label = QLabel("📋 系统日志管理")
        title_label.setStyleSheet("""
            QLabel {
                font-size: 20px;
                font-weight: bold;
                color: black;
                background-color: white;
                border: none;
            }
        """)
        header_layout.addWidget(title_label)
        header_layout.addStretch()
        
        export_btn = QPushButton("📤 导出日志")
        export_btn.setStyleSheet("""
            QPushButton {
                background-color: #007bff;
                color: white;
                border: none;
                padding: 8px 20px;
                border-radius: 4px;
                font-weight: bold;
                font-size: 14px;
            }
            QPushButton:hover {
                background-color: #0056b3;
            }
        """)
        header_layout.addWidget(export_btn)
        
        main_layout.addLayout(header_layout)
        
        # 三列日志布局
        logs_layout = QHBoxLayout()
        logs_layout.setSpacing(15)
        
        # 创建三个日志列
        self.create_communication_log(logs_layout)
        self.create_greeting_log(logs_layout)
        self.create_chat_log(logs_layout)
        
        main_layout.addLayout(logs_layout)
    
    def create_communication_log(self, layout):
        """创建沟通操作日志"""
        comm_group = QGroupBox("📞 沟通操作日志")
        comm_group.setStyleSheet("""
            QGroupBox {
                background-color: #e8f5e8;
                border: 2px solid #4caf50;
                border-radius: 8px;
                font-weight: bold;
                color: #2e7d32;
                margin-top: 8px;
                padding-top: 8px;
            }
            QGroupBox::title {
                subcontrol-origin: margin;
                left: 10px;
                padding: 0 5px 0 5px;
                color: #2e7d32;
            }
        """)
        comm_layout = QVBoxLayout(comm_group)
        
        # 创建滚动区域
        scroll_area = QScrollArea()
        scroll_area.setWidgetResizable(True)
        scroll_area.setStyleSheet("""
            QScrollArea {
                border: none;
                background-color: transparent;
            }
        """)
        
        # 日志内容区域
        log_widget = QWidget()
        log_layout = QVBoxLayout(log_widget)
        log_layout.setSpacing(5)
        
        # 示例日志数据
        comm_logs = [
            "2025-09-02 19:56:57: 批量先生沟通 等你不合适还是了打招呼了，我想我们清楚中了作！",
            "2025-09-02 19:56:54: 即先生 送了 不考虑，不符合要求",
            "2025-09-02 19:56:53: 港督和您先生进行沟通",
            "2025-09-02 19:56:48: 和刘女士沟通 发送了万便的话，到刘深圳沟通！",
            "2025-09-02 19:56:19: 港督和刘女士进行沟通",
            "2025-09-02 19:56:15: 港督和您先生进行沟通",
            "2025-09-02 19:56:15: 秘密到来吧，就心处理",
            "2025-09-02 19:56:12: 秘密到来吧，就心处理",
            "2025-09-02 19:56:11: 港督和你女士进行沟通",
            "2025-09-02 19:56:06: 批量先生沟通 等你不合适还是了打招呼了，我想我们清楚中了作！",
            "2025-09-02 19:56:02: 即先生 送了 不考虑，不符合要求",
            "2025-09-02 19:56:01: 港督和即先生进行沟通",
            "2025-09-02 19:55:53: 和刘女士沟通 等你不合适还是了打招呼了，我想我们清楚中了作！",
            "2025-09-02 19:55:49: 刘女士 送了 不考虑，不符合要求"
        ]
        
        for log_text in comm_logs:
            log_label = QLabel(log_text)
            log_label.setStyleSheet("""
                QLabel {
                    background-color: white;
                    border: 1px solid #e0e0e0;
                    border-radius: 4px;
                    padding: 8px;
                    margin: 2px;
                    color: #333;
                    font-size: 12px;
                }
            """)
            log_label.setWordWrap(True)
            log_layout.addWidget(log_label)
        
        log_layout.addStretch()
        scroll_area.setWidget(log_widget)
        comm_layout.addWidget(scroll_area)
        
        layout.addWidget(comm_group)
    
    def create_greeting_log(self, layout):
        """创建打招呼日志"""
        greeting_group = QGroupBox("👋 打招呼日志")
        greeting_group.setStyleSheet("""
            QGroupBox {
                background-color: #f3e5f5;
                border: 2px solid #9c27b0;
                border-radius: 8px;
                font-weight: bold;
                color: #7b1fa2;
                margin-top: 8px;
                padding-top: 8px;
            }
            QGroupBox::title {
                subcontrol-origin: margin;
                left: 10px;
                padding: 0 5px 0 5px;
                color: #7b1fa2;
            }
        """)
        greeting_layout = QVBoxLayout(greeting_group)
        
        # 创建滚动区域
        scroll_area = QScrollArea()
        scroll_area.setWidgetResizable(True)
        scroll_area.setStyleSheet("""
            QScrollArea {
                border: none;
                background-color: transparent;
            }
        """)
        
        # 日志内容区域
        log_widget = QWidget()
        log_layout = QVBoxLayout(log_widget)
        log_layout.setSpacing(5)
        
        # 示例日志数据
        greeting_logs = [
            "2025-09-02 14:26:28: 打招呼完成",
            "2025-09-02 14:25:18: 打招呼完成",
            "2025-09-02 14:24:00: 打招呼完成",
            "2025-09-02 14:22:40: 打招呼完成",
            "2025-09-02 14:21:21: 打招呼完成",
            "2025-09-02 14:20:05: 打招呼完成",
            "2025-09-02 14:18:48: 打招呼完成",
            "2025-09-02 14:18:00: 打招呼完成",
            "2025-09-02 14:16:43: 打招呼完成",
            "2025-09-02 14:15:31: 打招呼完成",
            "2025-09-02 14:06:55: 打招呼完成",
            "2025-09-02 14:05:16: 打招呼完成",
            "2025-09-02 14:03:57: 打招呼完成",
            "2025-09-02 14:03:15: 打招呼完成",
            "2025-09-02 14:01:55: 打招呼完成",
            "2025-09-02 14:00:35: 打招呼完成",
            "2025-09-02 13:59:14: 打招呼完成"
        ]
        
        for log_text in greeting_logs:
            log_label = QLabel(log_text)
            log_label.setStyleSheet("""
                QLabel {
                    background-color: white;
                    border: 1px solid #e0e0e0;
                    border-radius: 4px;
                    padding: 8px;
                    margin: 2px;
                    color: #333;
                    font-size: 12px;
                }
            """)
            log_label.setWordWrap(True)
            log_layout.addWidget(log_label)
        
        log_layout.addStretch()
        scroll_area.setWidget(log_widget)
        greeting_layout.addWidget(scroll_area)
        
        layout.addWidget(greeting_group)
    
    def create_chat_log(self, layout):
        """创建复聊日志"""
        chat_group = QGroupBox("🔄 复聊日志")
        chat_group.setStyleSheet("""
            QGroupBox {
                background-color: #fff3e0;
                border: 2px solid #ff9800;
                border-radius: 8px;
                font-weight: bold;
                color: #f57c00;
                margin-top: 8px;
                padding-top: 8px;
            }
            QGroupBox::title {
                subcontrol-origin: margin;
                left: 10px;
                padding: 0 5px 0 5px;
                color: #f57c00;
            }
        """)
        chat_layout = QVBoxLayout(chat_group)
        
        # 创建滚动区域
        scroll_area = QScrollArea()
        scroll_area.setWidgetResizable(True)
        scroll_area.setStyleSheet("""
            QScrollArea {
                border: none;
                background-color: transparent;
            }
        """)
        
        # 日志内容区域
        log_widget = QWidget()
        log_layout = QVBoxLayout(log_widget)
        log_layout.setSpacing(5)
        
        # 示例日志数据
        chat_logs = [
            "2025-09-02 12:00:54: 开始复聊",
            "2025-09-02 12:00:23: 开始复聊",
            "2025-09-02 11:59:52: 开始复聊",
            "2025-09-02 11:59:24: 开始复聊",
            "2025-09-02 11:58:52: 开始复聊",
            "2025-09-02 11:58:29: 客对女士上班接复聊信息",
            "2025-09-02 11:58:16: 开始复聊",
            "2025-09-02 11:57:29: 开始复聊",
            "2025-09-02 11:57:05: 开始复聊",
            "2025-09-02 11:56:37: 开始复聊",
            "2025-09-02 11:56:09: 开始复聊",
            "2025-09-02 11:55:45: 开始复聊",
            "2025-09-02 11:55:17: 开始复聊",
            "2025-09-02 11:54:50: 开始复聊",
            "2025-09-02 11:54:25: 开始复聊",
            "2025-09-02 11:53:57: 开始复聊",
            "2025-09-02 11:53:31: 开始复聊"
        ]
        
        for log_text in chat_logs:
            log_label = QLabel(log_text)
            log_label.setStyleSheet("""
                QLabel {
                    background-color: white;
                    border: 1px solid #e0e0e0;
                    border-radius: 4px;
                    padding: 8px;
                    margin: 2px;
                    color: #333;
                    font-size: 12px;
                }
            """)
            log_label.setWordWrap(True)
            log_layout.addWidget(log_label)
        
        log_layout.addStretch()
        scroll_area.setWidget(log_widget)
        chat_layout.addWidget(scroll_area)
        
        layout.addWidget(chat_group)
    
    def setup_timer(self):
        """设置定时器用于模拟日志更新"""
        self.timer = QTimer()
        self.timer.timeout.connect(self.update_logs)
        # 每30秒更新一次日志（可以根据需要调整）
        self.timer.start(30000)
    
    def update_logs(self):
        """更新日志内容（可以连接到实际的日志系统）"""
        # 这里可以添加实际的日志更新逻辑
        pass
